package 第二章数据结构.并查集.id836并查集;

import java.util.Scanner;

public class Main {
    static int[]p;
    public static void main(String[]args){
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int m = scanner.nextInt();
        //记录每个集合的顶层父节点，初始时为自己
         p = new int[n+1];
        for(int i = 1;i<= n;i++){
            p[i] = i;
        }


        scanner.nextLine();
        while (m-- > 0){
            String s = scanner.nextLine();
            String[] op = s.split(" ");
            int a = Integer.parseInt(op[1]);
            int b = Integer.parseInt(op[2]);
            if(op[0].equals("M")){
                merge(a,b);
            }else if(op[0].equals("Q")){
                if(query(a,b)){
                    System.out.println("Yes");
                }else
                    System.out.println("No");
            }
        }
    }

    public static void merge(int a, int b){
       p[find(a)] = find(b);
    }

    public static boolean query(int a, int b){
        return find(a) == find(b);

    }
    //返回x所在集合的编号，返回x的祖宗节点 + 路径压缩
    public static int find(int x){
        if(p[x] != x){
            p[x] = find(p[x]);
        }
        return p[x];
    }

}
